#include <iostream>

#include "VTTAtlas.h"
#include "VTTCommon.h"
#include "VTTErrorManager.h"
#include "VTTConfiguration.h"
#include "VTTImage.h"
#include "VTTTimer.h"
#include "VTTTileStore.h"
#include "VTTMemoryAllocator.h"
#include "VTTMemoryManager.h"

#include "VTTTileStore.h"
#include "VTTInfoFile.h"

#include <string>
#include <vector>

using std::cout;
using std::endl;
using std::string;
using std::vector;

bool tile_store_write = true;

void callbackFunction( bool result )
{
	cout << "Callback called! :-)" << endl;
	cout << "Result: " << result << endl;

	tile_store_write = false;
}

int main( int argc, char* argv[] )
{
	cout << "- VTTools by Michael Birsak -" << endl << endl;

	if (argc < 2)
	{
		cout << "No Configuration File specified" << endl;
		return 0;
	}

// 	float kernel[4];
// 
// 	VTTTileStore::GetGaussianFilterKernel( 1, 4, 1.0, kernel );

// 	unsigned char image[] = { 255, 255, 255,   128, 128, 128,     0,0,0,    64,64,64,
// 								0,0,0,   128,128,128,    64,64,64,   192,192,192,
// 							   32,32,32,   255,255,255,    64,64,64,   255,255,255,
// 							   64,64,64,     0,0,0,   128,128,128,   128,128,128 };
// 
// 	unsigned char dest[12];
// 
// 	VTTTileStore::ScaleRGBImageToTheHalf( dest, image, 4 );

	VTTTimer timer;

	timer.Toc();

	VTTConfiguration vttConfiguration( argv[1] );
	VTTAtlas atlas( vttConfiguration );
	atlas.Write();

	atlas.WriteOffsetFile();

	VTTTileStore tilestore( atlas );
	tilestore.Write();
/*	tilestore.Update(callbackFunction);*/

	//int number = 0;

	//double time = 0.0;

	//timer.Toc();

	cout << "Yeah, TS is ready!" << endl;

	string error = VTTErrorManager::Get()->popErrorMessage();

	while (error != "")
	{
		cout << error << endl;
		error = VTTErrorManager::Get()->popErrorMessage();
	}

	//tilestore.Update();
	
	cout << "INFO: Absolute Time needed: " << timer.Toc() << "s" << endl;
	//cout << "Files Read: " << VTTImage::filesRead << endl;
	/*cout << "Time needed by libjpeg-turbo: " << VTTImage::timeForLib << endl;
	cout << "Time needed by memcpy: " << VTTImage::timeForMemcpy << endl;
	cout << "Time needed by Malloc: " << VTTImage::timeForMalloc << endl;
	cout << "Time needed by deletion: " << VTTImage::timeForDeletion << endl;*/

	/*cout << "Memory available: "
		 << (VTTMemoryManager::Get()->GetFreePhysicalMemory() / 1024.0) / 1024.0
		 << endl;*/

	cout << endl;

	return 0;
}